/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link
            class="back"
            :to="{name:'friend_link_list',params:{currentPage:$route.params.currentPage}}"
            title="返回列表"
          >
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>友情链接管理 - 新增/编辑友情链接</h3>
            <h5>网站系统友情链接管理</h5>
          </div>
        </div>
      </div>
      <el-form ref="form" :model="form" label-width="200px">
        <el-form-item label="链接名称" required>
          <el-input v-model="form.link_name" size="mini"></el-input>
          <p class="err-msg" v-if="errResult.link_name && !form.link_name">{{errResult.link_name}}</p>
          <p class="notic">请填写友情链接的名称</p>
        </el-form-item>
        <el-form-item label="链接地址" required>
          <el-input v-model="form.link_url" size="mini"></el-input>
          <p class="err-msg" v-if="errResult.link_url && !form.link_url">{{errResult.link_url}}</p>
          <p class="notic">友情链接跳转地址</p>
        </el-form-item>
        <el-form-item label="排序" required>
          <el-input
            v-model="form.orderby"
            onkeyup="this.value=this.value.replace(/[^\d.]/g,'');"
            maxlength="3"
            size="mini"
          ></el-input>
          <p
            class="err-msg"
            v-if="errResult.orderby && (!form.orderby || form.orderby > 255)"
          >{{errResult.orderby}}</p>
          <p class="notic">请填写0-255的自然数，友情链接会根据排序进行由小到大排列显示</p>
        </el-form-item>
        <el-form-item label="链接logo">
          <el-upload
            class="avatar-uploader"
            :action="apiHead + '/mall/upload?type=logo'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <el-image
              v-if="form.link_logo"
              :src="form.link_logo"
              style="width: 240px; height: 60px"
            ></el-image>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <p class="notic">默认网站LOGO,通用头部显示，最佳显示尺寸为240*60像素</p>
        </el-form-item>
        <el-form-item label="是否新窗口打开">
          <el-switch v-model="form.target" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch v-model="form.is_show" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="isLoading">确认提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getFriendLink, updateFriendLink, addFriendLink } from "@/utils/api";
export default {
  data() {
    return {
      form: {},
      errResult: {},
      isLoading: false
    };
  },
  created() {
    if (this.$route.params.id) {
      this.getFriendLink();
    }
  },
  methods: {
    getFriendLink() {
      var that = this;
      getFriendLink(this.$route.params.id).then(function(res) {
        res.target = String(res.target);
        res.is_show = String(res.is_show);
        that.form = res;
      });
    },
    handleAvatarSuccess(res) {
      this.form.link_logo = res.result;
      this.$forceUpdate();
    },
    beforeAvatarUpload(file) {
      const isPic =
        file.type === "image/gif" ||
        file.type === "image/jpg" ||
        file.type === "image/jpeg" ||
        file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isPic) {
        this.$message.error("上传图片只能是 JPG、JPEG、PNG、GIF 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isPic && isLt2M;
    },
    onSubmit() {
      this.isLoading = true;
      var that = this;
      if (this.form.link_id) {
        //编辑
        updateFriendLink(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "保存成功",
              onClose: function() {
                that.$router.push({ name: "friend_link_list" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      } else {
        //添加
        addFriendLink(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "添加成功",
              onClose: function() {
                that.$router.push({ name: "friend_link_list" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      }
    }
  }
};
</script>

<style scoped>
.avatar-uploader-icon {
  line-height: 100px;
}
>>> .el-input__inner {
  width: 300px;
}
</style>
